home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 1.toast / pc / sample code / graphics 2d / quickdraw fx / fx.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  14.1 KB  |  580 lines

  1. /*
  2.     File:        FX.c
  3.  
  4.     Contains:    
  5.  
  6.     Written by: Edgar Lee    
  7.  
  8.     Copyright:    Copyright © 1992-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/13/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23.  
  24. #include "FX.h"
  25.  
  26. void transferExample();
  27. void arithmeticExample();
  28. void colorizationExample();
  29. void ditherExample();
  30. void mappingExample();
  31. void paintBucketExample();
  32. void lassoToolExample();
  33. void pixelAverageExample();
  34. void customExample();
  35.  
  36. GrafPtr    CreateGrafPort();
  37. void    DisposeGrafPort();
  38. int setTransferMode(int     item );
  39. int setArithmeticMode(int item);
  40. int setDitherMode(int item);
  41. pascal Boolean matchProc(RGBColor *color, long        *position );
  42. pascal Boolean searchProc();
  43.  
  44. long drawFXImage()
  45. {
  46.     int        exampleNumber, exampleItem;
  47.     Rect    rect, rect2;
  48.     Rect    outlineRect;
  49.     long    startTicks, endTicks;
  50.     Point    point = { 0, 0 };
  51.     Rect    frame;
  52.     
  53.     SetRect( &rect, (*gWindow).portRect.right - (*gGWorld).portRect.right - 20, 37,
  54.                 (*gWindow).portRect.right - 20, 37 + (*gGWorld).portRect.bottom );
  55.     
  56.     outlineRect = rect;
  57.     InsetRect( &outlineRect, -5, -5 );
  58.     drawDeepBox( &outlineRect );
  59.     
  60.     frame = rect;
  61.     
  62.     if (settings.bItem == 1)
  63.     {
  64.         ForeColor( whiteColor );
  65.         PaintRect( &rect );
  66.     }
  67.     else if (settings.bItem == 2)
  68.     {
  69.         ForeColor( blackColor );
  70.         PaintRect( &rect );
  71.     }
  72.     else
  73.     {
  74.         rect2 = rect;
  75.         
  76.         rect2.right = (rect2.right - rect2.left) / 2 + rect2.left;
  77.         ForeColor( whiteColor );
  78.         PaintRect( &rect2 );
  79.         
  80.         rect2.left = rect2.right;
  81.         rect2.right = rect.right;
  82.         ForeColor( blackColor );
  83.         PaintRect( &rect2 );
  84.     }
  85.  
  86.     ForeColor( blackColor );
  87.     BackColor( whiteColor );
  88.     
  89.     exampleNumber = gCurrentExample / 10;
  90.     exampleItem = gCurrentExample % 10;
  91.     
  92.     startTicks = TickCount();
  93.     
  94.     if (exampleNumber == 1)
  95.         transferExample( &rect, exampleItem );
  96.     else if (exampleNumber == 2)
  97.         arithmeticExample( &rect, exampleItem );
  98.     else if (exampleNumber == 3)
  99.         ditherExample( &rect, exampleItem );
  100.     else if (exampleNumber == 4)
  101.         colorizationExample( &rect, exampleItem );
  102.     else if (exampleNumber == 5)
  103.         mappingExample( &rect, exampleItem );
  104.     else if (exampleNumber == 6)
  105.         paintBucketExample( &rect, exampleItem, point );
  106.     else if (exampleNumber == 7)
  107.         lassoToolExample( &rect, exampleItem, &frame );
  108.     else if (exampleNumber == 8)
  109.         pixelAverageExample( &rect, exampleItem );
  110.     else if (exampleNumber == 9)
  111.         customExample( &rect, exampleItem );
  112.         
  113.     endTicks = TickCount();
  114.     
  115.     return (endTicks - startTicks);
  116. }
  117.  
  118. void transferExample( rect, item )
  119. Rect    *rect;
  120. int        item;
  121. {
  122.     int        transferMode;
  123.     
  124.     resetItems();
  125.     settings.tItem = item;
  126.     
  127.     transferMode = setTransferMode( item );
  128.     
  129.     CopyBits( (BitMap *)(*(*gGWorld).portPixMap), &gWindow->portBits,
  130.                 &(**(*gGWorld).portPixMap).bounds, rect, transferMode, nil );
  131. }
  132.  
  133. int setTransferMode(int     item )
  134. {
  135.     item--;
  136.     return item;
  137. }
  138.  
  139. void arithmeticExample( rect, item )
  140. Rect    *rect;
  141. int        item;
  142. {
  143.     int            arithmeticMode;
  144.     RGBColor    color;
  145.     
  146.     resetItems();
  147.     settings.aItem = item;
  148.     
  149.     arithmeticMode = setArithmeticMode( item );
  150.     
  151.     CopyBits( (BitMap *)(*(*gGWorld).portPixMap), &gWindow->portBits,
  152.                 &(**(*gGWorld).portPixMap).bounds, rect, arithmeticMode, nil );
  153.                 
  154.     color.red = color.green = color.blue = 0;
  155.     OpColor( &color );
  156. }
  157.  
  158. int setArithmeticMode(int item)
  159. {
  160.     RGBColor    color;
  161.     
  162.     if (item <= 4)
  163.         item = 31 + item;
  164.     else if (item == 9)
  165.         item = transparent;
  166.     else
  167.         item = 32 + item;
  168.     
  169.     color.red = color.green = color.blue = 0xffff / 2;
  170.     OpColor( &color );
  171.     
  172.     return item;
  173. }
  174.  
  175. void colorizationExample(Rect    * rect,int item )
  176. {
  177.     int            i;
  178.     Rect        srcRect, dstRect;
  179.     RGBColor    color;
  180.     
  181.     resetItems();
  182.     settings.cItem = item;
  183.     
  184.     srcRect = (**(*gGWorld).portPixMap).bounds;
  185.     srcRect.right = srcRect.left + (srcRect.right - srcRect.left) / 3;
  186.     
  187.     dstRect = *rect;
  188.     dstRect.right = dstRect.left + (dstRect.right - dstRect.left) / 3;
  189.     
  190.     for (i = 0; i < 3; i++)
  191.     {
  192.         if (item == 1)
  193.         {
  194.             ForeColor( blackColor );
  195.             BackColor( whiteColor );
  196.         }    
  197.         else if (item == 2)
  198.         {
  199.             ForeColor( whiteColor );
  200.             BackColor( blackColor );
  201.         }
  202.         else
  203.         {
  204.             color.red = color.green = color.blue = 0;
  205.         
  206.             if (i == 0)
  207.                 color.red = 0xffff;
  208.             else if (i == 1)
  209.                 color.green = 0xffff;
  210.             else if (i == 2)
  211.                 color.blue = 0xffff;
  212.             
  213.             if (item == 3)
  214.             {
  215.                 RGBForeColor( &color );
  216.                 BackColor( whiteColor );
  217.             }
  218.             else
  219.             {
  220.                 RGBBackColor( &color );
  221.                 ForeColor( blackColor );
  222.             }
  223.         }
  224.                 
  225.         CopyBits( (BitMap *)(*(*gGWorld).portPixMap), &gWindow->portBits,
  226.                 &srcRect, &dstRect, srcCopy, nil );
  227.                 
  228.         OffsetRect( &srcRect, srcRect.right - srcRect.left, 0 );
  229.         OffsetRect( &dstRect, dstRect.right - dstRect.left, 0 );
  230.     }
  231. }
  232.  
  233. void ditherExample(Rect    * rect,int item )
  234. {
  235.     int        ditherMode;
  236.     
  237.     /* This function shows an example of using copybits with the dithering. */
  238.     
  239.     resetItems();
  240.     settings.dItem = item;
  241.     
  242.     ditherMode = setDitherMode( item );
  243.     
  244.     CopyBits( (BitMap *)(*(*gGWorld).portPixMap), &gWindow->portBits,
  245.                 &(**(*gGWorld).portPixMap).bounds, rect, srcCopy + ditherMode, nil );
  246. }
  247.  
  248. int setDitherMode(int item)
  249. {
  250.     if (item == 1)
  251.         return 0;
  252.     else
  253.         return ditherCopy;
  254. }
  255.  
  256. void mappingExample(Rect * rect,int    item )
  257. {
  258.     int            i;
  259.     GWorldPtr    gworld;
  260.     CTabHandle    ictable;
  261.     
  262.     resetItems();
  263.     settings.mItem = item;
  264.     
  265.     if (item == 1)
  266.         CopyBits( (BitMap *)(*(*gGWorld).portPixMap), &gWindow->portBits,
  267.             &(**(*gGWorld).portPixMap).bounds, rect, srcCopy, nil );
  268.     else if (item == 2)
  269.     {
  270.         ColorSearchUPP searchProcUPP=NewColorSearchProc(searchProc);
  271.         AddSearch(searchProcUPP);
  272.         
  273.         CopyBits( (BitMap *)(*(*gGWorld).portPixMap), &gWindow->portBits,
  274.             &(**(*gGWorld).portPixMap).bounds, rect, srcCopy, nil );
  275.         
  276.         DelSearch(searchProcUPP);
  277.     }
  278.     else if (item == 3)
  279.     {
  280.         NewGWorld( &gworld, 8, &(*gGWorld).portRect, GetCTable( 8 + 64 ), nil, 0 );
  281.         
  282.         CopyBits( (BitMap *)(*(*gGWorld).portPixMap), (BitMap *)(*(*gworld).portPixMap),
  283.                     &(**(*gGWorld).portPixMap).bounds, &(**(*gworld).portPixMap).bounds,
  284.                     srcCopy, nil );
  285.         
  286.         ictable = GetCTable( 8 );
  287.         
  288.         for (i = 0; i <= (**(**(*gworld).portPixMap).pmTable).ctSize; i++)
  289.             (**(**(*gworld).portPixMap).pmTable).ctTable[i].rgb =
  290.                     (**ictable).ctTable[255 - i].rgb;
  291.         
  292.         (**(**(*gworld).portPixMap).pmTable).ctSeed = 67;
  293.     //            (**(**(*(CGrafPtr)gWindow).portPixMap).pmTable).ctSeed; 
  294.         
  295.         CopyBits( (BitMap *)(*(*gworld).portPixMap), &gWindow->portBits,
  296.             &(**(*gworld).portPixMap).bounds, rect, srcCopy, nil );
  297.         
  298.         DisposeGWorld( gworld );
  299.     }
  300. }
  301.  
  302. pascal Boolean searchProc( RGBColor    * color, long        *position )
  303. {
  304.     #pragma unused(position)
  305.     (*color).red = 0;
  306.     
  307.     return false;
  308. }
  309.  
  310. void paintBucketExample(Rect *rect, exampleItem, Point point )
  311. {
  312.     #pragma unused(exampleItem)
  313.     GrafPtr     mask;
  314.     Point        newPoint;
  315.     GWorldPtr    gworld;
  316.     
  317.     resetItems();
  318.  
  319.     newPoint.h = point.h - 20;
  320.     newPoint.v = point.v - 37;
  321.  
  322.     if (newPoint.h < 0 || newPoint.v < 0 ||
  323.         newPoint.h > ((**(*gGWorld).portPixMap).bounds.right - (**(*gGWorld).portPixMap).bounds.left) ||
  324.         newPoint.v > ((**(*gGWorld).portPixMap).bounds.bottom - (**(*gGWorld).portPixMap).bounds.top))
  325.         return;
  326.         
  327.     mask = CreateGrafPort( &(*gGWorld).portRect );
  328.     
  329.     ForeColor( redColor );
  330.     MoveTo( point.h - 2, point.v );
  331.     LineTo( point.h + 2, point.v );
  332.     MoveTo( point.h, point.v - 2 );
  333.     LineTo( point.h, point.v + 2 );
  334.  
  335.     ForeColor( blackColor );
  336.  
  337.     NewGWorld( &gworld, 8, &(*gGWorld).portRect, GetCTable( 8 + 64 ), nil, 0 );
  338.         
  339.     CopyBits( (BitMap *)(*(*gGWorld).portPixMap), (BitMap *)(*(*gworld).portPixMap),
  340.                     &(**(*gGWorld).portPixMap).bounds, &(**(*gworld).portPixMap).bounds,
  341.                     srcCopy, nil );
  342.                     
  343. //    GetGWorld( ¤tPort, ¤tDevice );
  344. //    SetGWorld( gworld, nil );
  345.     
  346.     SeedCFill( (BitMap *)(*(*gworld).portPixMap), &mask->portBits,
  347.                 &(**(*gworld).portPixMap).bounds, &mask->portRect,
  348.                 newPoint.h, newPoint.v, nil, 0 );
  349. //    SetGWorld( currentPort, currentDevice );
  350.     
  351.     CopyMask( (BitMap *)(*(*gworld).portPixMap), &mask->portBits, &gWindow->portBits,
  352.                 &(**(*gworld).portPixMap).bounds, &mask->portRect, rect );
  353.     
  354.     DisposeGrafPort( mask );
  355. }
  356.  
  357. void lassoToolExample(Rect    * rect, int    exampleItem,Rect *frame )
  358. {
  359.     #pragma unused(exampleItem,frame)
  360.     GrafPtr     mask;
  361.     RGBColor    color;
  362.     CGrafPtr    currentPort;
  363.     GDHandle    currentDevice;
  364.     
  365.     pascal Boolean matchProc();
  366.     
  367.     resetItems();
  368.  
  369.     mask = CreateGrafPort( &(*gGWorld).portRect );
  370.  
  371.     GetGWorld( ¤tPort, ¤tDevice );
  372.     SetGWorld( gGWorld, nil );
  373.     
  374.     color.red = color.green = color.blue = 0;
  375.     
  376.     CalcCMask( (BitMap *)(*(*gGWorld).portPixMap), &mask->portBits,
  377.                 &(**(*gGWorld).portPixMap).bounds, &mask->portRect,
  378.                 &color, NewColorSearchProc(matchProc), 0 );
  379.     SetGWorld( currentPort, currentDevice );
  380.     
  381.     CopyMask( (BitMap *)(*(*gGWorld).portPixMap), &mask->portBits, &gWindow->portBits,
  382.                 &(**(*gGWorld).portPixMap).bounds, &mask->portRect, rect );
  383. /*
  384.     bounds = *frame;
  385.     OffsetRect( &bounds, -bounds.left, -bounds.top );
  386.     
  387.     mask = CreateGrafPort( &bounds );
  388.  
  389.     GetGWorld( ¤tPort, ¤tDevice );
  390.     SetGWorld( gGWorld, nil );
  391.     
  392.     color.red = color.green = color.blue = 0;
  393.     
  394.     CalcCMask( (BitMap *)(*(*gGWorld).portPixMap), &mask->portBits,
  395.                 &bounds, &mask->portRect, &color, matchProc, 0 );
  396.                 
  397.     SetGWorld( currentPort, currentDevice );
  398.         
  399.     CopyMask( (BitMap *)(*(*gGWorld).portPixMap), &mask->portBits, &gWindow->portBits,
  400.                 &bounds, &mask->portRect, frame );
  401. */
  402.     
  403.     DisposeGrafPort( mask );
  404. }
  405.  
  406. pascal Boolean matchProc(RGBColor *color, long        *position )
  407. {    
  408.     GDHandle    currDevice;
  409.     MatchRec    *matchInfo;
  410.     
  411.     currDevice = GetGDevice();
  412.     matchInfo = (MatchRec *)(**currDevice).gdRefCon;
  413.  
  414.     if (matchInfo->red == color->red && matchInfo->green == color->green &&
  415.         matchInfo->blue == color->blue)
  416.         *position = 0;
  417.     else
  418.         *position = 1;
  419.         
  420.     return true;
  421. }
  422.  
  423. void pixelAverageExample(Rect    *rect, int     item )
  424. {
  425.     #pragma unused(item)
  426.     Rect        bounds;
  427.     GWorldPtr    gworld;
  428.     GWorldPtr    source;
  429.     
  430.     resetItems();
  431.     
  432.     /* Create 8-bit gworld from 32-bit original. */
  433.     
  434.     NewGWorld( &source, 8, &(*gGWorld).portRect, GetCTable( 8 + 64 ), nil, 0 );
  435.     CopyBits( (BitMap *)(*(*gGWorld).portPixMap), (BitMap *)(*(*source).portPixMap),
  436.             &(**(*gGWorld).portPixMap).bounds, &(**(*source).portPixMap).bounds,
  437.             srcCopy, nil );
  438.     
  439.     /* Create another 8-bit gworld at quarter size of original. */
  440.         
  441.     SetRect( &bounds, 0, 0, (*gGWorld).portRect.right / 4,
  442.                 (*gGWorld).portRect.bottom / 4 );
  443.     
  444.     NewGWorld( &gworld, 8, &bounds, GetCTable( 8 + 64 ), nil, 0 );
  445.  
  446.     /* Copy the 8-bit original to 8-bit quarter image without pixel averaging. */
  447.     
  448.     bounds = (*source).portRect;
  449.     
  450.     CopyBits( (BitMap *)(*(*source).portPixMap), (BitMap *)(*(*gworld).portPixMap),
  451.             &(**(*source).portPixMap).bounds, &(**(*gworld).portPixMap).bounds,
  452.             srcCopy, nil );
  453.     
  454.     /* Copy the quarter image to screen. */
  455.     
  456.     OffsetRect( &bounds, 20, 37 );
  457.     CopyBits( (BitMap *)(*(*gworld).portPixMap), &(*gWindow).portBits,
  458.             &(**(*gworld).portPixMap).bounds, &bounds, srcCopy, nil );
  459.     
  460.     /* Now, copy the 8-bit original to 8-bit quarter image with pixel averaging. */
  461.     
  462.     bounds = (*source).portRect;
  463.     
  464.     CopyBits( (BitMap *)(*(*source).portPixMap), (BitMap *)(*(*gworld).portPixMap),
  465.             &(**(*source).portPixMap).bounds, &(**(*gworld).portPixMap).bounds,
  466.             srcCopy + ditherCopy, nil );
  467.     
  468.     /* Finally, copy pixel averaged quarter image to screen. */
  469.             
  470.     OffsetRect( &bounds, (*rect).left, (*rect).top );
  471.     CopyBits( (BitMap *)(*(*gworld).portPixMap), &(*gWindow).portBits,
  472.             &(**(*gworld).portPixMap).bounds, &bounds, srcCopy, nil );
  473.     
  474.     DisposeGWorld( source );
  475.     DisposeGWorld( gworld );
  476. }
  477.  
  478. void customExample(Rect    * rect,int    item )
  479. {
  480.     #pragma unused(item)
  481.     int            transferMode;
  482.     int            ditherMode;
  483.     RGBColor    color;
  484.     ColorSearchUPP searchProcUPP=NewColorSearchProc(searchProc);
  485.         
  486.     transferMode = setTransferMode( settings.tItem );
  487.     ditherMode = setDitherMode( settings.dItem );
  488.     
  489.     if (settings.cItem == 1)
  490.     {
  491.         ForeColor( blackColor );
  492.         BackColor( whiteColor );
  493.     }
  494.     else if (settings.cItem == 2)
  495.     {
  496.         ForeColor( whiteColor );
  497.         BackColor( blackColor );
  498.     }
  499.     else if (settings.cItem == 3)
  500.     {
  501.         color.green = 0xffff;
  502.         color.red = color.blue = 0;
  503.         RGBForeColor( &color );
  504.         BackColor( whiteColor );
  505.     }
  506.     else if (settings.cItem == 4)
  507.     {
  508.         color.green = 0xffff;
  509.         color.red = color.blue = 0;
  510.         ForeColor( blackColor );
  511.         RGBBackColor( &color );
  512.     }
  513.     
  514.     
  515.     if (settings.mItem == 2)
  516.         AddSearch(searchProcUPP);
  517.     
  518.     
  519.     CopyBits( (BitMap *)(*(*gGWorld).portPixMap), &gWindow->portBits,
  520.                 &(**(*gGWorld).portPixMap).bounds, rect,
  521.                 transferMode + ditherMode, nil );
  522.                 
  523.     if (settings.mItem == 2)
  524.         DelSearch(searchProcUPP);
  525. }
  526.  
  527. GrafPtr CreateGrafPort( bounds )    /* Originally written by Forrest Tanaka. */
  528. Rect *bounds;
  529. {
  530.     GrafPtr    savedPort;        /* Saved GrafPtr for later restore. */
  531.     GrafPtr    newPort;        /* New GrafPort. */
  532.     Rect    localBounds;    /* Local copy of bounds. */
  533.  
  534.     GetPort( &savedPort );
  535.  
  536.     /* Set the top-left corner of bounds to (0,0). */
  537.     localBounds = *bounds;
  538.     OffsetRect( &localBounds, -bounds->left, -bounds->top );
  539.  
  540.     /* Allocate a new GrafPort. */
  541.     newPort = (GrafPtr)NewPtrClear( sizeof( GrafPort ) );
  542.     
  543.     if (newPort != nil)
  544.     {
  545.         /* Initialize the new port and make the current port. */
  546.         OpenPort( newPort );
  547.  
  548.         /* Initialize and allocate the bitmap. */
  549.         newPort->portBits.bounds = localBounds;
  550.           newPort->portBits.rowBytes = ((localBounds.right + 15) >> 4) << 1;
  551.         newPort->portBits.baseAddr =  NewPtrClear( newPort->portBits.rowBytes *
  552.                                                     (long)localBounds.bottom );
  553.         if (newPort->portBits.baseAddr != nil)
  554.         {
  555.             /* Clean up the new port. */
  556.             newPort->portRect = localBounds;
  557.             ClipRect( &localBounds );
  558.             RectRgn( newPort->visRgn, &localBounds );
  559.             EraseRect( &localBounds );
  560.         }
  561.         else
  562.         {
  563.             /* Allocation failed; deallocate the port. */
  564.             ClosePort( newPort );
  565.             DisposePtr( (Ptr)newPort );
  566.             newPort = nil;
  567.         }
  568.     }
  569.     
  570.     SetPort( savedPort );
  571.     return newPort;
  572. }
  573.  
  574. void DisposeGrafPort( doomedPort )        /* Originally written by Forrest Tanaka. */
  575. GrafPtr doomedPort;
  576. {
  577.     ClosePort( doomedPort );
  578.     DisposePtr( doomedPort->portBits.baseAddr );
  579.     DisposePtr( (Ptr)doomedPort );
  580. }